package com.csw.tourismInformation.service;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.csw.tourismInformation.dao.ScenicDao;
import com.csw.tourismInformation.entity.Scenic;

/**
 * Created by Administrator on 2019/10/23.
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ScenicServiceImpl implements ScenicService {
	@Autowired
	private ScenicDao scenicDao;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Integer countPage(Integer pageSize, Integer pid) {
		Integer countPage = 0;
		Integer count = scenicDao.queryCount(pid);
		if (count % pageSize == 0) {
			countPage = count / pageSize;
		} else {
			countPage = count / pageSize + 1;
		}
		return countPage;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public List<Scenic> queryAllAndByPage(Integer currPage, Integer pageSize, Integer countPage, Integer pid) {
		Integer firstIndex;
		Integer lastIndex;
		if (!currPage.equals(countPage)) {
			firstIndex = (currPage - 1) * pageSize + 1;
			lastIndex = currPage * pageSize;
		} else {
			firstIndex = (currPage - 1) * pageSize + 1;
			lastIndex = scenicDao.queryCount(pid);
		}
		return scenicDao.queryAllAndByPage(firstIndex, lastIndex, pid);
	}

	@Override
	public void deleteView(Integer pid) {
		scenicDao.deleteView(pid);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public Scenic queryById(Integer pid) {
		return scenicDao.queryById(pid);
	}

	@Override
	public void updateView(Scenic scenic, File cover) {
		scenicDao.updateView(scenic, cover);
	}

	@Override
	public void addView(Scenic scenic) {
		scenicDao.addView(scenic);
	}
}
